package com.weifly.weistock.record.bill;

import com.weifly.weistock.record.base.domain.MergeRecordResult;
import com.weifly.weistock.record.base.domain.RecordPair;
import com.weifly.weistock.record.bill.domain.StockRecordDto;
import com.weifly.weistock.record.bill.domain.StockSummaryDto;

/**
 * 账单工具方法
 *
 * @author weifly
 * @since 2020/01/15
 */
public class BillUtils {

    public static String descRecord(StockRecordDto record){
        StringBuilder sb = new StringBuilder();
        sb.append("date=").append(record.getDate());
        sb.append(", time=").append(record.getTime());
        sb.append(", businessName=").append(record.getBusinessName().getName());
        sb.append(", stockCode=").append(record.getStockCode());
        sb.append(", stockName=").append(record.getStockName());
        sb.append(", tradePrice=").append(record.getTradePrice());
        sb.append(", tradeNumber=").append(record.getTradeNumber());
        sb.append(", tradeAmount=").append(record.getTradeAmount());
        sb.append(", afterNumber=").append(record.getAfterNumber());
        sb.append(", feeService=").append(record.getFeeService());
        sb.append(", feeStamp=").append(record.getFeeStamp());
        sb.append(", entrustCode=").append(record.getEntrustCode());
        return sb.toString();
    }

    /**
     * 是卖出操作，则返回true
     */
    public static boolean isCloseRecord(StockRecordDto record){
        BillConst.BusinessName businessName = record.getBusinessName();
        // 证券卖出
        if(BillConst.BusinessName.NAME_2.equals(businessName)){
            return true;
        }
        // 担保品划出
        if(BillConst.BusinessName.NAME_4.equals(businessName)){
            return true;
        }
        // 还款卖出
        if(BillConst.BusinessName.NAME_6.equals(businessName)){
            return true;
        }
        return false;
    }

    public static boolean isOpenRecord(StockRecordDto record){
        BillConst.BusinessName businessName = record.getBusinessName();
        // 证券买入
        if(BillConst.BusinessName.NAME_1.equals(businessName)){
            return true;
        }
        // 担保品划入
        if(BillConst.BusinessName.NAME_3.equals(businessName)){
            return true;
        }
        // 融资买入
        if(BillConst.BusinessName.NAME_5.equals(businessName)){
            return true;
        }
        // 红股入帐
        if(BillConst.BusinessName.NAME_8.equals(businessName)){
            return true;
        }
        return false;
    }

    /**
     * 获得未配对成交量
     */
    public static int getNotPairNumber(StockRecordDto record){
        int totalPairNumber = 0;
        if(record.getPairList()!=null){
            for(RecordPair pair : record.getPairList()){
                totalPairNumber += pair.getNumber();
            }
        }
        return record.getTradeNumber() - totalPairNumber;
    }

    public static void addRecordToSummary(StockSummaryDto summaryDto, StockRecordDto recordDto, MergeRecordResult result){
        // key规则：发生日期_成交时间_委托编号
        String key = recordDto.getDate() + "_" + recordDto.getTime() + "_" + recordDto.getEntrustCode();
        StockRecordDto targetDto = summaryDto.getRecordMap().get(key);
        if(targetDto==null){
            summaryDto.getRecordList().add(recordDto);
            summaryDto.getRecordMap().put(key, recordDto);
            if(result!=null){
                result.setInsert(result.getInsert()+1);
            }
        }else{
            copyRecordProp(recordDto, targetDto);
            if(result!=null){
                result.setUpdate(result.getUpdate()+1);
            }
        }
    }

    public static void copyRecordProp(StockRecordDto source, StockRecordDto target) {
        // 共17个属性
        target.setDate(source.getDate());
        target.setTime(source.getTime());
        target.setBusinessName(source.getBusinessName());
        target.setStockCode(source.getStockCode());
        target.setStockName(source.getStockName());
        target.setTradePrice(source.getTradePrice());
        target.setTradeNumber(source.getTradeNumber());
        target.setTradeAmount(source.getTradeAmount());
        target.setAfterNumber(source.getAfterNumber());
        target.setFeeService(source.getFeeService());
        target.setFeeStamp(source.getFeeStamp());
        target.setFeeTransfer(source.getFeeTransfer());
        target.setFeeExtra(source.getFeeExtra());
        target.setFeeClear(source.getFeeClear());
        target.setClearAmount(source.getClearAmount());
        target.setAfterAmount(source.getAfterAmount());
        target.setEntrustCode(source.getEntrustCode());

        target.setPairList(source.getPairList());
        target.setDiffAmount(source.getDiffAmount());
    }
}
